package com.vpen.blog.common;

import java.io.Serializable;

/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/7/7 13:38
 * -@Content: 前端访问  返回的结果对象 是否成功
 */
public class ReturnResult implements Serializable {
    // 饿汉单例
    private static volatile ReturnResult instance=null;
    private int code; // 状态码
    private String msg;// 提示信息
    private Object data;  // 返回的数据


    private  ReturnResult(){}
    public static ReturnResult getInstance(){
        if (instance == null) {
            synchronized (ReturnResult.class){
                if (instance == null) {
                    instance=new ReturnResult();
                }
            }
        }
        return instance;
    }

    private  ReturnResult unionFunc(int code,String msg,Object data){
        instance.setCode(code);
        instance.setMsg(msg);
        instance.setData(data);
        return instance;
    }
    // 请求成功
    // 请求成功
    public  ReturnResult success(){
        return this.unionFunc(200,"请求成功",null);
    }
    public  ReturnResult success(Object data){
        return this.unionFunc(200,"请求成功",data);
    }
    // 请求成功
    public  ReturnResult success(String msg,Object data){
        return this.unionFunc(200,msg,data);
    }

    // 请求失败
    public  ReturnResult fail(){
        return this.unionFunc(400,"失败",null);
    }
    // 请求失败
    public  ReturnResult fail(String msg){
        return this.unionFunc(400,msg,null);
    }



    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ReturnResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
